/**
 * 电子病历管理初始化
 */
var CdcEmr = {
    id: "CdcEmrTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    editor:null
};

/**
 * 初始化表格的列
 */
CdcEmr.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        // {title: '就诊记录', field: 'episodeId', align: 'center', valign: 'middle', sortable: true},
        {title: '索引码', field: 'xcode', align: 'center', valign: 'middle', sortable: true},
        // {title: '文档类型', field: 'busEmrTypeId', align: 'center', valign: 'middle', sortable: true},
        {title: '标题', field: 'title', align: 'left', valign: 'middle', sortable: true},
        {title: '子标题', field: 'subTitle', align: 'left', valign: 'middle', sortable: true},
        {title: '内容', field: 'content', align: 'left', valign: 'middle', sortable: true},
        {title: '填写日期', field: 'emrDate', align: 'left', valign: 'middle', sortable: true},
        {title: '填写人', field: 'emrUserId', align: 'left', valign: 'middle', sortable: true}
        // {title: '业务数据类型', field: 'dataId', align: 'center', valign: 'middle', sortable: true},
        // {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true},
        // {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
    	// 	var _html = '<a href="javascript:CdcEmr.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    	// 	_html += '<a href="javascript:CdcEmr.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    	// 	return _html;
        // }}
    ];
};

/**
 * 检查是否选中
 */
CdcEmr.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcEmr.seItem = selected[0];
        return true;
    }
};

CdcEmr.ClickRow = function (r,e,f){
    var resultData;
    // 获取解析结果并显示
    var ajax = new $ax(Sys.backendPath + "/cdc/cdcbsemrstruct/getStructResult", function (data) {
        resultData = data;
    }, function (data) {
    });
    ajax.set({"emrId":r.id});
    ajax.start();

    if (resultData != null) {
        var htmlStr="";
        for (var i=0, l=r.content.length; i<l; i++) {
            htmlStr+="<span>"+r.content.charAt(i)+"</span>"
        }
        document.getElementById("emrContent").innerHTML = htmlStr;
        var jsonStr = $.parseJSON(resultData.emrNlpResult)
        CdcEmr.editor.set(jsonStr);
        for (var j=0, m=jsonStr.length; j < m; j++) {
            var resultItem = jsonStr[j];
            var indexArr=resultItem.index;
            var tagType=resultItem.type;
            for (var k=0, n=indexArr.length;k<n;k++) {
                var indexStart = indexArr[k][0];
                var indexEnd = indexArr[k][1];
                for (var tmpIndex=indexStart; tmpIndex<=indexEnd;tmpIndex++) {
                    $("#emrContent").children().eq(tmpIndex).addClass(tagType);
                }
            }
        }
    } else {
        document.getElementById("emrContent").innerHTML = r.content;
        CdcEmr.editor.set();
    }
};

/**
 * 点击新增电子病历
 */
CdcEmr.add = function () {
    var index = Sys.open('新增电子病历',Sys.backendPath + '/cdc/cdcemr/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看电子病历详情
 */
CdcEmr.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('电子病历详情',Sys.backendPath + '/cdc/cdcemr/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除电子病历
 */
CdcEmr.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdcemr/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcEmr.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcEmrId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除电子病历?",operation);
    }
};

/**
 * 查询电子病历列表
 */
CdcEmr.search = function () {
    var queryData = {};
    queryData['xcode'] = $("#xcode").val();
    queryData['busEmrTypeId'] = $("#busEmrTypeId").val();
    queryData['title'] = $("#title").val();
    queryData['subTitle'] = $("#subTitle").val();
    queryData['content'] = $("#content").val();
    queryData['emrDate'] = $("#emrDate").val();
    queryData['emrUserId'] = $("#emrUserId").val();
    queryData['dataId'] = $("#dataId").val();
    queryData['isActive'] = $("#isActive").val();
    CdcEmr.table.refresh({query: queryData});
};

/**
 * by xzl
 */
CdcEmr.query = function () {
    if ((EpisodeIDTable.selEpisode)&&(EpisodeIDTable.selEpisode.id)) {
        var queryData = '{"NotQuery":"false","episodeId":"' + EpisodeIDTable.selEpisode.id + '"}';
        queryData = JSON.parse(queryData);
        CdcEmr.table.clear();
        CdcEmr.table.refresh({ query: queryData });
    }else{
        // CdcExamination.table.clear();
        Sys.info('请先选择患者！' );
    }
};

CdcEmr.colorList = {
    "+symptom": {"name":"症状","color":"rgb(255, 0, 0)"},
    "+sign": {"name":"体征","color":"rgb(0, 153, 153)"},
    "+Drug": {"name":"药物","color":"rgb(255, 51, 255)"},
    "+personalhistory": {"name":"个人史", "color":"rgb(204, 204, 51)"},
    "+operation": {"name":"手术","color":"rgb(51, 153, 153)"},
    "+disease": {"name":"疾病","color":"rgb(0, 255, 0)"},
    "+allergyhistory": {"name":"过敏史","color":"rgb(0, 153, 255)"},
    "+complication": {"name":"并发症","color":"rgb(255, 102, 153)"},
    "Lis_Order": {"name":"检验项目","color":"rgb(230, 104, 88)"},
    "+ris_result": {"name":"检查结果","color":"rgb(159, 230, 207)"},
    "nursing_assessment": {"name":"护理措施","color":"rgb(172, 230, 128)"},
    "+familyhistory": {"name":"家族史","color":"rgb(85, 153, 255)"}
}

$(function () {
    var defaultColunms = CdcEmr.initColumn();
    var table = new BSTable(CdcEmr.id, Sys.backendPath + "/cdc/cdcemr/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(20);
    table.setOnClickRow(CdcEmr.ClickRow);
    table.setHeight("580")
    CdcEmr.table = table.init();
    CdcEmr.query();

    var container = document.getElementById("jsoneditor");
    var options = {};
    var editor = new JSONEditor(container, options);
    CdcEmr.editor = editor;
});
